package com.example.tellwin.question.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.tellwin.R;
import com.example.tellwin.utils.GlideUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author : LinZePeng
 * @date : 2020/11/10
 * @Describe ：
 **/
public class PhotoAdapter extends RecyclerView.Adapter<PhotoAdapter.PhotoViewHolder> {


    List<String> photoPaths = new ArrayList<>();

    Context context;

    PhotoClickListener photoClickListener;

    public void setPhotoClickListener(PhotoClickListener photoClickListener) {
        this.photoClickListener = photoClickListener;
    }

    public void setPhotoPaths(List<String> photoPaths) {
        this.photoPaths = photoPaths;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public PhotoViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        context = parent.getContext();
        return new PhotoViewHolder(LayoutInflater.from(context).inflate(R.layout.item_photo, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull PhotoViewHolder holder, int position) {
        GlideUtils.LoadImage(context, photoPaths.get(position), holder.itemPhotoIv);
        holder.deletePhotoIv.setOnClickListener(v -> {
            if(photoClickListener!=null){
                photoClickListener.onDelteClick(position);
            }
        });
        holder.itemPhotoIv.setOnClickListener(v -> {
            if(photoClickListener!=null){
                photoClickListener.onItemClick(position ,photoPaths.get(position));
            }
        });
    }

    @Override
    public int getItemCount() {
        return photoPaths.size();
    }


    class PhotoViewHolder extends RecyclerView.ViewHolder {

        ImageView itemPhotoIv;
        ImageView deletePhotoIv;

        public PhotoViewHolder(@NonNull View itemView) {
            super(itemView);
            itemPhotoIv = (ImageView) itemView.findViewById(R.id.item_photo_iv);
            deletePhotoIv = (ImageView) itemView.findViewById(R.id.delete_photo_iv);
        }
    }

    public interface PhotoClickListener{
        void onDelteClick(int position);
        void onItemClick(int position , String path);
    }
}
